package com.jnu.accounthelper.ui.gallery;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CalendarView;
import android.widget.Gallery;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AlertDialog;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;


import com.jnu.accounthelper.R;
import com.jnu.accounthelper.databinding.FragmentGalleryBinding;
import com.jnu.accounthelper.db.Account;
import com.jnu.accounthelper.db.DBManager;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class GalleryFragment extends Fragment {

    private GalleryViewModel galleryViewModel;
    private FragmentGalleryBinding binding;

    private CalendarView calendarView;
    private RecyclerView historyView;
    private TextView calendar;

    private ArrayList<Account> accounts;
    private GalleryFragment.MyRecyclerViewAdapter adapter;
    @RequiresApi(api = Build.VERSION_CODES.O)
    public View onCreateView(@NonNull LayoutInflater inflater,
                             ViewGroup container, Bundle savedInstanceState) {
        galleryViewModel =
                new ViewModelProvider(this).get(GalleryViewModel.class);

        binding = FragmentGalleryBinding.inflate(inflater, container, false);
        View root = binding.getRoot();
        calendar=root.findViewById(R.id.calendar);
        //获取当前时间
        LocalDate date = LocalDate.now();
        accounts=DBManager.SelectByDay(date.getYear()+"年"+date.getMonth().getValue()+"月"+date.getDayOfMonth()+"日",0);

        calendarView =(CalendarView) root.findViewById(R.id.calenderview);
        historyView=root.findViewById(R.id.history_recycle);

        historyView = root.findViewById(R.id.history_recycle);
        historyView.setLayoutManager(new LinearLayoutManager(this.getContext()));
        adapter = new GalleryFragment.MyRecyclerViewAdapter(accounts);
        historyView.setAdapter(adapter);
        calendar.setText(date.getYear()+"年"+date.getMonth().getValue()+"月"+date.getDayOfMonth()+"日");
        //日历监听事件
        calendarView.setOnDateChangeListener(new CalendarView.OnDateChangeListener() {
            @Override
            public void onSelectedDayChange(@NonNull CalendarView View, int year, int month, int day) {
                int m =month+1;
                accounts= DBManager.SelectByDay(year,m,day);
                adapter.notifyDataSetChanged();
                calendar.setText(year+"年"+m+"月"+day+"日");
            }
        });

        return root;
    }

    private class MyRecyclerViewAdapter extends RecyclerView.Adapter{
        //private List<Account> accounts;


        public MyRecyclerViewAdapter(List<Account> accounts) {
            //this.accounts=accounts;
        }
        @NonNull
        //加载视图
        @Override
        public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            View view;
            view = LayoutInflater.from(parent.getContext())
                    .inflate(R.layout.account_item_holder, parent, false);
            return new GalleryFragment.MyRecyclerViewAdapter.MyViewHolder(view);


        }

        @Override
        public void onBindViewHolder(@NonNull RecyclerView.ViewHolder Holder, int position) {
            GalleryFragment.MyRecyclerViewAdapter.MyViewHolder holder =(GalleryFragment.MyRecyclerViewAdapter.MyViewHolder)Holder;
            holder.getImageView().setImageResource(accounts.get(position).getCoverResourceId());
            holder.getType().setText(accounts.get(position).getType());
            holder.getNumber().setText(accounts.get(position).getNumber()+"");
            holder.getRemark().setText(accounts.get(position).getRemark());
            holder.getDate().setText(accounts.get(position).getDate());
        }



        @Override
        public int getItemCount() {
            return accounts.size();
        }

        private class MyViewHolder extends RecyclerView.ViewHolder{

            private final ImageView imageView;
            private final TextView type;
            private final TextView remark;
            private final TextView number;
            private final TextView date;

            public MyViewHolder(View view) {
                super(view);

                this.imageView=view.findViewById(R.id.image_view_book_cover);
                this.type=view.findViewById(R.id.type);
                this.remark=view.findViewById(R.id.remark);
                this.number=view.findViewById(R.id.number);
                this.date =view.findViewById(R.id.item_date);

            }

            public TextView getDate() {
                return date;
            }

            public ImageView getImageView() {
                return imageView;
            }

            public TextView getType() {
                return type;
            }

            public TextView getRemark() {
                return remark;
            }

            public TextView getNumber() {
                return number;
            }





        }
    }
    @Override
    public void onDestroyView() {
        super.onDestroyView();
        binding = null;
    }
}